CC=mpicc
CXX=mpicxx
LD=mpicxx
LIBS=-lrt
ifeq ($(DEBUG), yes)
CFLAGS:=-DINLINE="static inline" -O0 -fno-strict-aliasing -g -I. -fno-exceptions
OBJDIR:=../obj_dbg
EXE=$(EXEDIR)/esmd.debug
else
CFLAGS:=-DINLINE="static inline" -O3 -fno-strict-aliasing -g -I. -fno-exceptions -fno-tree-loop-distribute-patterns
CXXFLAGS=$(CFLAGS) -std=gnu++17
SWGCC=$(shell mpicc -v 2>&1 | grep -E sw.gcc -o | head -n 1)
ifeq ($(SWGCC), sw5gcc)
SCC=sw5gcc
SCXX=sw5g++
SLD=sw5ld
CFLAG_HOST=-mhost
CFLAG_SLAVE=-mslave -fstack-reuse=all #-mlra
CFLAGS:=$(CFLAGS) -mieee-with-inexact -funroll-loops -ffast-math -msimd -D__sw__ -D__sw5__ -D__sw_256bit_simd__ -D__sw_regcomm__ -faddress_align=32 -mfma -dp -I../qthread/ #-funroll-nested-loops
ARCH:=sw5
LDFLAGS:=
MPIINC:=$(subst /bin/mpicc,/include,$(shell which mpicc))
LIBS:=-L../qthread -lqthread 
LD:=python3 sw/xlink.py mpicxx
endif
ifeq ($(SWGCC), sw7gcc)
SCC=sw7gcc
SCXX=sw7g++
SLD=sw7ld
CFLAG_HOST=-mhost -mlra -mfp-regs
CFLAG_SLAVE=-mslave #-mlra
CFLAGS:=$(CFLAGS) -mieee -msimd -D__sw__ -D__sw7__ -D__sw_512bit_simd__ -D__sw_rldst__ -faddress_align=32 -mfma -funroll-loops -dp -I../qthread/ #-funroll-nested-loops
ARCH:=sw7
LDFLAGS:=-Wl,-q
MPIINC:=$(subst /bin/mpicc,/include,$(shell which mpicc))
LIBS:=-L../qthread -lqthread
endif
ifeq ($(SWGCC), sw9gcc)
SCC=sw9gcc
SCXX=sw9g++
SLD=sw9ld
CFLAG_HOST=-mhost
CFLAG_SLAVE=-mslave #-mlra
CFLAGS:=$(CFLAGS) -mieee -msimd -D__sw__ -D__sw9__ -D__sw_512bit_simd__ -D__sw_rldst__ -faddress_align=32 -mfma -funroll-loops -dp -I../qthread/ #-funroll-nested-loops
ARCH:=sw9
LDFLAGS:=-Wl,-q
MPIINC:=$(subst /bin/mpicc,/include,$(shell which mpicc))
LIBS:=-L../qthread -lqthread
endif
OBJDIR=../obj
EXE=$(EXEDIR)/esmd
endif
EXEDIR=../bin
DEPDIR=$(OBJDIR)/.dep
MKOBJDIR:=$(shell if [ ! -d $(OBJDIR) ]; then mkdir $(OBJDIR); fi)
MKDEPDIR:=$(shell if [ ! -d $(DEPDIR) ]; then mkdir $(DEPDIR); fi)
MKEXEDIR:=$(shell if [ ! -d $(EXEDIR) ]; then mkdir $(EXEDIR); fi)
LIBS:= $(LIBS) -lm
SRC:=$(wildcard *.c) $(wildcard *.cpp)
ifeq ($(ARCH), sw5)
SRC:= $(SRC) $(wildcard sw/*.c) $(wildcard sw/*.cpp)
ASM:= $(wildcard sw/*.S)
SWDIR=$(OBJDIR)/sw
MKSWDIR:=$(shell if [ ! -d $(SWDIR) ]; then mkdir $(SWDIR); fi)
MKSWDEPDIR:=$(shell if [ ! -d $(DEPDIR)/sw ]; then mkdir $(DEPDIR)/sw; fi)

LIBS:=$(LIBS) -lm_slave
endif
ifeq ($(ARCH), sw7)
SRC:= $(SRC) $(wildcard sw/*.c) $(wildcard sw/*.cpp)
#ASM:= $(wildcard sw/*.S)
SWDIR=$(OBJDIR)/sw
MKSWDIR:=$(shell if [ ! -d $(SWDIR) ]; then mkdir $(SWDIR); fi)
MKSWDEPDIR:=$(shell if [ ! -d $(DEPDIR)/sw ]; then mkdir $(DEPDIR)/sw; fi)

LIBS:=$(LIBS) -lm_slave
endif
ifeq ($(ARCH), sw9)
SRC:= $(SRC) $(wildcard sw/*.c) $(wildcard sw/*.cpp)
#ASM:= $(wildcard sw/*.S)
SWDIR=$(OBJDIR)/sw
MKSWDIR:=$(shell if [ ! -d $(SWDIR) ]; then mkdir $(SWDIR); fi)
MKSWDEPDIR:=$(shell if [ ! -d $(DEPDIR)/sw ]; then mkdir $(DEPDIR)/sw; fi)

LIBS:=$(LIBS) -lm_slave
endif

DEP:= $(patsubst %.cpp, $(DEPDIR)/%.dep, $(patsubst %.c, $(DEPDIR)/%.dep, $(SRC)))
OBJ:= $(patsubst %.cpp, $(OBJDIR)/%.o, $(patsubst %.c, $(OBJDIR)/%.o, $(SRC)) $(patsubst %.S, $(OBJDIR)/%.o, $(ASM)))

all: $(EXE)
$(DEPDIR)/%.dep: %.c
	$(CC) -D__sw_host__ -D__sw_slave__ -MT $(OBJDIR)/$*.o -MM -MG $< -I. > $@
$(DEPDIR)/%.dep: %.cpp
	$(CXX) -D__sw_host__ -D__sw_slave__ -MT $(OBJDIR)/$*.o -MM -MG $< -I. > $@
ifeq ($(SWGCC), sw5gcc)
$(SWDIR)/%.o: sw/%.c
	$(CC) $(CFLAG_HOST) -I$(MPIINC) $(CFLAGS) -c $< -o $(SWDIR)/$*.host.o
	$(SCC) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS) -c $< -o $(SWDIR)/$*.slave.o
	$(SLD) -r $(SWDIR)/$*.host.o $(SWDIR)/$*.slave.o -o $@
$(SWDIR)/%.o: sw/%.cpp
	$(CXX) $(CFLAG_HOST) -I$(MPIINC) $(CXXFLAGS)  -c $< -o $(SWDIR)/$*.host.o
	$(SCXX) $(CFLAG_SLAVE) -I$(MPIINC) $(CXXFLAGS)  -c $< -o $(SWDIR)/$*.slave.o
	$(SLD) -r $(SWDIR)/$*.host.o $(SWDIR)/$*.slave.o -o $@
$(SWDIR)/%_cpe.o: sw/%_cpe.S
	$(SCC) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS) -c $< -o $@
endif
ifeq ($(SWGCC), sw7gcc)
$(SWDIR)/%.o: sw/%.c
	$(CC) $(CFLAG_HOST) -I$(MPIINC) $(CFLAGS) -c $< -o $(SWDIR)/$*.host.o
	$(SCC) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS) -c $< -o $(SWDIR)/$*.slave.o
	$(SLD) -r $(SWDIR)/$*.host.o $(SWDIR)/$*.slave.o -o $@
$(SWDIR)/%.o: sw/%.cpp
	$(CXX) $(CFLAG_HOST) -I$(MPIINC) $(CFLAGS)  -c $< -o $(SWDIR)/$*.host.o
	$(SCXX) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS)  -c $< -o $(SWDIR)/$*.slave.o
	$(SLD) -r $(SWDIR)/$*.host.o $(SWDIR)/$*.slave.o -o $@
$(SWDIR)/%_cpe.o: sw/%_cpe.S
	$(SCC) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS) -c $< -o $@
endif
ifeq ($(SWGCC), sw9gcc)
$(SWDIR)/%.o: sw/%.c
	$(CC) $(CFLAG_HOST) -I$(MPIINC) $(CFLAGS) -c $< -o $(SWDIR)/$*.host.o
	$(SCC) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS) -c $< -o $(SWDIR)/$*.slave.o
	$(SLD) -r $(SWDIR)/$*.host.o $(SWDIR)/$*.slave.o -o $@
$(SWDIR)/%.o: sw/%.cpp
	$(CXX) $(CFLAG_HOST) -I$(MPIINC) $(CFLAGS)  -c $< -o $(SWDIR)/$*.host.o
	$(SCXX) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS)  -c $< -o $(SWDIR)/$*.slave.o
	$(SLD) -r $(SWDIR)/$*.host.o $(SWDIR)/$*.slave.o -o $@
$(SWDIR)/%_cpe.o: sw/%_cpe.S
	$(SCC) $(CFLAG_SLAVE) -I$(MPIINC) $(CFLAGS) -c $< -o $@
endif
$(OBJDIR)/%.o: %.cpp
	$(CXX) $(CFLAGS) $< -c -o $@ -fpermissive
$(OBJDIR)/%.o: %.c
	$(CC) $(CFLAGS) $< -c -o $@
$(EXE): $(OBJ)
	$(LD) $(LDFLAGS) $^ -o $@ $(LIBS)
%.gen.h: generate_%.py
	python3 $< > $@
-include $(DEP)
.PHONY: clean
clean:
	-rm -f $(DEP) $(OBJ) $(EXE)
